//
//  HOMessageViewController.m
//  WeiDu_1_0
//
//  Created by guoweiquan on 16/1/15.
//  Copyright © 2016年 Shenou Tech. All rights reserved.
//

#import "HOMessageViewController.h"
#import "HOConstants.h"
#import "HOMessageEntity.h"
#import "HOUtils.h"
#import "HOMessageDataModule.h"
#import "HOReportDetailViewController.h"
#import "HOAddExtraAssistantViewController.h"
#import "HOExtraAssistantEntity.h"
#import "HOUIMgr.h"

@interface HOMessageViewController ()
{
}
@end
@implementation HOMessageViewController
- (void)viewDidLoad {
	[super viewDidLoad];
	[self initTableView];
	self.tableViewDataList = ((HOMessageDataModule *)dataModule).lastMessageList;
	[self requestDataWithMaxInd:1];
	self.noResultView.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"消息", @"消息")];
	
}

- (void)requestDataWithMaxInd:(int)maxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetMyMessages,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}

- (void)pullUpToRefresh
{
	HOMessageEntity *entity = self.tableViewDataList.lastObject;
	if(entity)
	{
		[self requestDataWithMaxInd:entity.messageId];
	}
	else
	{
		[self requestDataWithMaxInd:1];
	}
}

- (void)pullDownToRefresh
{
	[self requestDataWithMaxInd:1];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];
    return count;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
    HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[HOTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
    }
	HOMessageEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	UIImageView *headerImgView = (UIImageView *)[cell viewWithTag:115];
	if(headerImgView == nil)
	{
		headerImgView = [[[UIImageView alloc]
						 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												  HO_HORIZONAL_PADDING,
												  38,
												  38)] autorelease];
		headerImgView.image = [UIImage imageNamed:@"ic_wodexiaoxi_xitongxiaoxi"];
		headerImgView.tag = 115;
		[cell.contentView addSubview:headerImgView];
	}
	
	
	UILabel *titleLbl = (UILabel *)[cell viewWithTag:116];
	if(titleLbl == nil)
	{
		titleLbl = [[[UILabel alloc]
						 initWithFrame:CGRectMake(58,
												  8,
												  tableView.frame.size.width - 58 - 140,
												  22)] autorelease];
		titleLbl.tag = 116;
		titleLbl.textColor = TEXT_COLOR_181818;
//		titleLbl.layer.borderWidth = 1;
		titleLbl.font = [UIFont systemFontOfSize:15];
		[cell.contentView addSubview:titleLbl];
	}
	titleLbl.text = entity.title;
	
	
	UILabel *contentLgbl = (UILabel *)[cell viewWithTag:117];
	if(contentLgbl == nil)
	{
		contentLgbl = [[[UILabel alloc]
						 initWithFrame:CGRectMake(titleLbl.frame.origin.x,
												  33,
												  tableView.frame.size.width - HO_HORIZONAL_PADDING - 58 - 30,
												  37)] autorelease];
		contentLgbl.tag = 117;
		contentLgbl.textColor = TEXT_COLOR_999999;
//		contentLgbl.layer.borderWidth = 1;
		contentLgbl.numberOfLines = 2;
		contentLgbl.font = [UIFont systemFontOfSize:13];
		[cell.contentView addSubview:contentLgbl];
	}
	contentLgbl.text = entity.message;
	
	
	UILabel *timeLbl = (UILabel *)[cell viewWithTag:119];
	if(timeLbl == nil)
	{
		timeLbl = [[[UILabel alloc]
						 initWithFrame:CGRectMake(tableView.frame.size.width - 140,
												  8,
												  130,
												  22)] autorelease];
		timeLbl.textColor = TEXT_COLOR_999999;
		timeLbl.textAlignment = NSTextAlignmentRight;
//		timeLbl.layer.borderWidth = 1;
		timeLbl.font = [UIFont systemFontOfSize:13];
		timeLbl.tag = 119;
		[cell.contentView addSubview:timeLbl];
	}
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.createTime * 1000];
	HOMessageEntity *message = [self.tableViewDataList objectAtIndex:indexPath.section];
	int orderId = message.orderId;
	if(orderId == 0)
	{
		
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	else
	{
		
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOMessageEntity *message = [self.tableViewDataList objectAtIndex:indexPath.row];
	if(message.orderType == HO_REPORT_TYPE_EXTRAASSIS)
	{
		HOAddExtraAssistantViewController *viewCtrl = [[HOAddExtraAssistantViewController alloc] init];
		HOExtraAssistantEntity *entity = [[[HOExtraAssistantEntity alloc] init] autorelease];
		entity.orderId = message.orderId;
		entity.companyId = message.companyId;
		viewCtrl.isDetailDisplayModal = YES;
		viewCtrl.extraAssistantEntity = entity;
		
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	else
	{
		int orderId = message.orderId;
		HOReportDetailViewController *viewCtrl = [[HOReportDetailViewController alloc] init];
		viewCtrl.reportType = message.orderType;
		HOReportOrderEntity *entity = [[[HOReportOrderEntity alloc] init] autorelease];
		entity.orderId = orderId;
		viewCtrl.orderEntity = entity;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
    
}
#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetMyMessages])
	{
		[self doneLoadingTableViewRefresh];
	}
}
@end
